//
//  Box2DArray.m
//  MinusTheBox
//
//  Created by Chi Ho Chiu on 15/4/12.
//  Copyright 2012 Individual. All rights reserved.
//

#import "Box2DArray.h"


@implementation Box2DArray
@synthesize boxPosArray;

-(void) dealloc
{
	[boxPosArray release];
	[super dealloc];
}

-(id) init
{
	if( self = [super init] )
	{		
		//Init the boxPosArray with fixed initial size
		self.boxPosArray = [NSMutableArray arrayWithCapacity:MAX_BOX_COLUMN];
		for(int i=0; i<MAX_BOX_COLUMN; i++)
		{
			NSMutableArray *rowArray = [NSMutableArray arrayWithCapacity:MAX_BOX_ROW];
			for(int j=0; j<MAX_BOX_ROW; j++)
			{
				[rowArray insertObject:[NSNull null] atIndex:j];
			}
			[boxPosArray addObject:rowArray];
		}
	}
	
	return self;
}

-(id) getObjectInColumn:(int)col row:(int)row
{
	return [[boxPosArray objectAtIndex:col] objectAtIndex:row];
}

-(void) updateObjectInColumn:(int)col row:(int)row value:(id)val
{
	[self addObjectInColumn:col row:row value:val];
}

-(void) addObjectInColumn:(int)col row:(int)row value:(id)val
{
	[[boxPosArray objectAtIndex:col] replaceObjectAtIndex:row withObject:val];
}

-(void) removeObjectInColumn:(int)col row:(int)row
{
	[self addObjectInColumn:col row:row value:[NSNull null]];
}

-(BOOL) isAllNull
{
	for(int col=0; col<MAX_BOX_COLUMN; col++)
	{
		NSMutableArray *rowArray = [boxPosArray objectAtIndex:col];
		for(int row=0; row<MAX_BOX_ROW; row++)
		{
			if( !([[rowArray objectAtIndex:row] isEqual:[NSNull null]]) )
				return NO;
		}
	}
	
	return YES;
}

@end
